package com.industrial.premu.service.impl;

import cn.hutool.core.util.IdUtil;
import com.alibaba.excel.EasyExcel;
import com.industrial.common.core.utils.DateUtils;
import com.industrial.common.core.utils.poi.ExcelUtil;
import com.industrial.common.core.web.domain.AjaxResult;
import com.industrial.common.security.utils.SecurityUtils;
import com.industrial.premu.domain.PreMuPioneerDeveZone;
import com.industrial.premu.domain.PremuPioneerCompany;
import com.industrial.premu.domain.PremuPioneerCounty;
import com.industrial.premu.domain.vo.PreMuPioneerCountVo;
import com.industrial.premu.mapper.PreMuPioneerDeveZoneMapper;
import com.industrial.premu.service.IPreMuPioneerDeveZoneService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PreMuPioneerDeveZoneServiceImpl implements IPreMuPioneerDeveZoneService {

    @Autowired
    private PreMuPioneerDeveZoneMapper preMuPioneerDeveZoneMapper;

    /**
     * 查询开发区先锋
     *
     * @param baseId 开发区先锋主键
     * @return 开发区先锋
     */
    @Override
    public PreMuPioneerDeveZone selectPreMuPioneerDeveZoneByBaseId(String baseId)
    {
        return preMuPioneerDeveZoneMapper.selectPreMuPioneerDeveZoneByBaseId(baseId);
    }

    /**
     * 查询开发区先锋列表
     *
     * @param preMuPioneerDeveZone 开发区先锋
     * @return 开发区先锋
     */
    @Override
    public List<PreMuPioneerDeveZone> selectPreMuPioneerDeveZoneList(PreMuPioneerDeveZone preMuPioneerDeveZone)
    {
        return preMuPioneerDeveZoneMapper.selectPreMuPioneerDeveZoneList(preMuPioneerDeveZone);
    }

    @Override
    public List<PreMuPioneerDeveZone> listTop10(PreMuPioneerDeveZone preMuPioneerDeveZone) {
        return preMuPioneerDeveZoneMapper.listTop10(preMuPioneerDeveZone);
    }

    /**
     * 新增开发区先锋
     *
     * @param preMuPioneerDeveZone 开发区先锋
     * @return 结果
     */
    @Override
    public int insertPreMuPioneerDeveZone(PreMuPioneerDeveZone preMuPioneerDeveZone)
    {
        return preMuPioneerDeveZoneMapper.insertPreMuPioneerDeveZone(preMuPioneerDeveZone);
    }

    /**
     * 修改开发区先锋
     *
     * @param preMuPioneerDeveZone 开发区先锋
     * @return 结果
     */
    @Override
    public int updatePreMuPioneerDeveZone(PreMuPioneerDeveZone preMuPioneerDeveZone)
    {
        return preMuPioneerDeveZoneMapper.updatePreMuPioneerDeveZone(preMuPioneerDeveZone);
    }

    /**
     * 批量删除开发区先锋
     *
     * @param baseIds 需要删除的开发区先锋主键
     * @return 结果
     */
    @Override
    public int deletePreMuPioneerDeveZoneByBaseIds(String[] baseIds)
    {
        return preMuPioneerDeveZoneMapper.deletePreMuPioneerDeveZoneByBaseIds(baseIds);
    }

    /**
     * 删除开发区先锋信息
     *
     * @param baseId 开发区先锋主键
     * @return 结果
     */
    @Override
    public int deletePreMuPioneerDeveZoneByBaseId(String baseId)
    {
        return preMuPioneerDeveZoneMapper.deletePreMuPioneerDeveZoneByBaseId(baseId);
    }

    /**
     * 导入开发区数据
     *
     * @param file
     * @param years
     * @return
     */
    @Override
    public AjaxResult importPioneerDeveZoneData(MultipartFile file, String years) {
        if(file.isEmpty()){
            return AjaxResult.warn("没有找到要上传的文件");
        }
        try{
            ExcelUtil<PreMuPioneerDeveZone> excelUtil = new ExcelUtil<PreMuPioneerDeveZone>(PreMuPioneerDeveZone.class);
            List<PreMuPioneerDeveZone> preMuPioneerDeveZones = excelUtil.importExcel(file.getInputStream(),1);

            List<PreMuPioneerDeveZone> list = new ArrayList<>();

            for(PreMuPioneerDeveZone t : preMuPioneerDeveZones){
                t.setBaseId(IdUtil.simpleUUID());
                t.setBaseCreateTime(DateUtils.getNowDate());
                t.setBaseCreateBy(SecurityUtils.getUserId());
                t.setBaseUpdateTime(DateUtils.getNowDate());
                t.setBaseUpdateBy(SecurityUtils.getUserId());

                list.add(t);
                if(list.size() > 100){
                    preMuPioneerDeveZoneMapper.batchInsertPioneerDeveZone(list);
                    list.clear();
                }
            }

            if(!list.isEmpty()){
                preMuPioneerDeveZoneMapper.batchInsertPioneerDeveZone(list);
                list.clear();
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        return AjaxResult.success("导入成功");
    }

    /**
     * 查询统计数量
     *
     * @param years
     * @return
     */
    @Override
    public PreMuPioneerCountVo queryTjCount(String years) {
        return preMuPioneerDeveZoneMapper.queryTjCount(years);
    }

    /**
     * 先锋企业排名
     *
     * @param years
     * @return
     */
    @Override
    public Map<String,List<String>> queryTjCompanyOrderEcharts(String years,String industry) {
        List<PremuPioneerCompany> r = preMuPioneerDeveZoneMapper.queryTjCompanyOrderEcharts(years,industry);
        List<String> xAxis = new ArrayList<>();
        List<String> yAxis = new ArrayList<>();
        for(PremuPioneerCompany p : r){
            String companyName = p.getCompanyName();
            String scords = p.getScords();
            xAxis.add(companyName);
            yAxis.add(scords);
        }
        Map<String,List<String>> resultMap = new HashMap<>(16);
        resultMap.put("xAxis",xAxis);
        resultMap.put("yAxis",yAxis);
        return resultMap;
    }

    /**
     * 先锋区县排名
     *
     * @param years
     * @return
     */
    @Override
    public Map<String, List<String>> queryTjCountyOrderEcharts(String years) {
        List<PremuPioneerCounty> r = preMuPioneerDeveZoneMapper.queryTjCountyOrderEcharts(years);
        List<String> xAxis = new ArrayList<>();
        List<String> yAxis = new ArrayList<>();
        for(PremuPioneerCounty p : r){
            String companyName = p.getBaseName();
            String scords = p.getScores();
            xAxis.add(companyName);
            yAxis.add(scords);
        }
        Map<String,List<String>> resultMap = new HashMap<>(16);
        resultMap.put("xAxis",xAxis);
        resultMap.put("yAxis",yAxis);
        return resultMap;
    }

    /**
     * 先锋区县排名
     *
     * @param years
     * @return
     */
    @Override
    public Map<String, List<String>> queryTjKfqOrderEcharts(String years,String groupName) {
        List<PreMuPioneerDeveZone> r = preMuPioneerDeveZoneMapper.queryTjKfqOrderEcharts(years,groupName);
        List<String> xAxis = new ArrayList<>();
        List<String> yAxis = new ArrayList<>();
        for(PreMuPioneerDeveZone p : r){
            String companyName = p.getKfqCode();
            String scords = p.getEvaluateScore();
            xAxis.add(companyName);
            yAxis.add(scords);
        }
        Map<String,List<String>> resultMap = new HashMap<>(16);
        resultMap.put("xAxis",xAxis);
        resultMap.put("yAxis",yAxis);
        return resultMap;
    }
}
